load("//xla:xla.default.bzl", "xla_cc_test")
load("//xla/pjrt/cpu:package_groups.bzl", "xla_cpu_internal_packages")
load("//xla/tsl:tsl.bzl", "internal_visibility")
load("//xla/tsl/platform:rules_cc.bzl", "cc_library")

package(
    # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
    default_visibility = ["//visibility:private"],
    licenses = ["notice"],
)

# Required to load package group definitions.
xla_cpu_internal_packages()

xla_cc_test(
    name = "pjrt_client_test_cpu",
    srcs = ["pjrt_client_test_cpu.cc"],
    deps = [
        ":cpu_client",
        "//xla/pjrt:pjrt_client_test_common",
        "//xla/tsl/platform:test_main",
    ],
)

xla_cc_test(
    name = "tracked_cpu_device_buffer_test",
    srcs = ["tracked_cpu_device_buffer_test.cc"],
    deps = [
        ":abstract_cpu_buffer",
        ":cpu_client",
        ":cpu_event",
        "//xla:util",
        "//xla/pjrt:pjrt_client",
        "//xla/tsl/concurrency:async_value",
        "//xla/tsl/concurrency:ref_count",
        "//xla/tsl/platform:env",
        "//xla/tsl/platform:statusor",
        "@com_google_absl//absl/log:check",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "abstract_cpu_buffer",
    srcs = [
        "abstract_cpu_buffer.cc",
        "raw_buffer.cc",
        "tracked_cpu_device_buffer.cc",
    ],
    hdrs = [
        "abstract_cpu_buffer.h",
        "raw_buffer.h",
        "tracked_cpu_device_buffer.h",
    ],
    visibility = internal_visibility(["//xla/pjrt/cpu:legacy_cpu_buffer_internal_users"]),
    deps = [
        ":cpu_event",
        "//xla:future",
        "//xla:literal",
        "//xla:shape_tree",
        "//xla:shape_util",
        "//xla:util",
        "//xla:xla_data_proto_cc",
        "//xla/backends/cpu:alignment",
        "//xla/pjrt:abstract_tracked_device_buffer",
        "//xla/pjrt:async_work_runner",
        "//xla/pjrt:common_pjrt_client",
        "//xla/pjrt:device_event",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:raw_buffer",
        "//xla/pjrt:transpose",
        "//xla/pjrt:utils",
        "//xla/service:shaped_buffer",
        "//xla/service/cpu:cpu_executable",
        "//xla/service/cpu:cpu_xfeed",
        "//xla/stream_executor:device_memory",
        "//xla/tsl/concurrency:async_value",
        "//xla/tsl/concurrency:ref_count",
        "//xla/tsl/platform:env",
        "//xla/tsl/platform:errors",
        "//xla/tsl/platform:statusor",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/base:no_destructor",
        "@com_google_absl//absl/container:inlined_vector",
        "@com_google_absl//absl/functional:any_invocable",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/types:span",
        "@tsl//tsl/platform:casts",
        "@tsl//tsl/platform:platform_port",
        "@tsl//tsl/profiler/lib:connected_traceme",
        "@tsl//tsl/profiler/lib:traceme",
    ],
)

cc_library(
    name = "cpu_device",
    srcs = ["cpu_device.cc"],
    hdrs = ["cpu_device.h"],
    visibility = internal_visibility(["//xla/pjrt/cpu:legacy_cpu_topology_users"]),
    deps = [
        ":cpu_async_execution_tracker",
        ":execution_stream_event_map",
        "//xla:literal",
        "//xla/pjrt:host_memory_spaces",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_common",
        "//xla/pjrt:semaphore",
        "//xla/pjrt/plugin/xla_cpu:cpu_device_description",
        "//xla/service/cpu:cpu_xfeed",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:inlined_vector",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "cpu_event",
    hdrs = ["cpu_event.h"],
    visibility = internal_visibility(["//xla/pjrt/cpu:legacy_cpu_client_users"]),
)

cc_library(
    name = "cpu_client",
    srcs = ["cpu_client.cc"],
    hdrs = ["cpu_client.h"],
    visibility = internal_visibility(["//xla/pjrt/cpu:legacy_cpu_client_users"]),
    deps = [
        ":abstract_cpu_buffer",
        ":cpu_async_execution_tracker",
        ":cpu_device",
        ":cpu_event",
        "//xla:array",
        "//xla:debug_options_flags",
        "//xla:executable_run_options",
        "//xla:future",
        "//xla:literal",
        "//xla:literal_util",
        "//xla:shape_util",
        "//xla:util",
        "//xla:xla_data_proto_cc",
        "//xla:xla_proto_cc",
        "//xla/backends/cpu:alignment",
        "//xla/backends/cpu:constant_allocation",
        "//xla/backends/cpu:target_machine_options",
        "//xla/backends/cpu/codegen:cpu_features",
        "//xla/backends/cpu/collectives:cpu_collectives",
        "//xla/backends/cpu/runtime:buffer_allocations",
        "//xla/backends/cpu/runtime:thread_pool_task_runner",
        "//xla/backends/cpu/runtime:thunk",
        "//xla/backends/cpu/runtime:thunk_executor",
        "//xla/backends/cpu/runtime:xfeed_manager",
        "//xla/client:executable_build_options",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/ir:hlo",
        "//xla/hlo/ir:hlo_module_group",
        "//xla/pjrt:abstract_tracked_device_buffer",
        "//xla/pjrt:async_work_runner",
        "//xla/pjrt:common_pjrt_client",
        "//xla/pjrt:device_event",
        "//xla/pjrt:host_callback",
        "//xla/pjrt:host_memory_spaces",
        "//xla/pjrt:layout_mode",
        "//xla/pjrt:mlir_to_hlo",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_common",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt:raw_buffer",
        "//xla/pjrt:semaphore",
        "//xla/pjrt:thread_pool_async_work_runner",
        "//xla/pjrt:transpose",
        "//xla/pjrt:utils",
        "//xla/pjrt/dump",
        "//xla/pjrt/plugin/xla_cpu:cpu_client_options",
        "//xla/pjrt/plugin/xla_cpu:cpu_execute_options",
        "//xla/pjrt/plugin/xla_cpu:cpu_memory",
        "//xla/pjrt/plugin/xla_cpu:cpu_topology",
        "//xla/pjrt/plugin/xla_cpu:cpu_topology_description",
        "//xla/pjrt/proto:compile_options_proto_cc",
        "//xla/service:buffer_assignment",
        "//xla/service:compiler",
        "//xla/service:computation_placer_hdr",
        "//xla/service:dump",
        "//xla/service:executable",
        "//xla/service:hlo_cost_analysis",
        "//xla/service:hlo_module_config",
        "//xla/service:hlo_module_util",
        "//xla/service:hlo_proto_cc",
        "//xla/service:hlo_value",
        "//xla/service:maybe_owning_device_address",
        "//xla/service/cpu:cpu_compiler",
        "//xla/service/cpu:cpu_executable",
        "//xla/service/cpu:cpu_executable_run_options",
        "//xla/service/cpu:executable_proto_cc",
        "//xla/service/llvm_ir:llvm_command_line_options",
        "//xla/stream_executor:device_memory",
        "//xla/tsl/concurrency:async_value",
        "//xla/tsl/concurrency:ref_count",
        "//xla/tsl/platform:env",
        "//xla/tsl/platform:errors",
        "//xla/tsl/platform:statusor",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/base:dynamic_annotations",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:inlined_vector",
        "@com_google_absl//absl/functional:any_invocable",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/types:span",
        "@eigen_archive//:eigen3",  # TODO(zhangqiaorjc): Remove if use TFRT threadpool.
        "@llvm-project//mlir:IR",
        "@tsl//tsl/platform:casts",
        "@tsl//tsl/platform:denormal",
        "@tsl//tsl/platform:fingerprint",
        "@tsl//tsl/platform:setround",
        "@tsl//tsl/profiler/lib:traceme",
    ],
)

xla_cc_test(
    name = "cpu_client_test",
    srcs = ["cpu_client_test.cc"],
    deps = [
        ":cpu_client",
        "//xla:literal",
        "//xla:literal_util",
        "//xla:shape_util",
        "//xla:types",
        "//xla:util",
        "//xla:xla_data_proto_cc",
        "//xla/ffi",
        "//xla/ffi:ffi_api",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/parser:hlo_parser",
        "//xla/mlir_hlo",
        "//xla/pjrt:host_memory_spaces",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt/plugin/xla_cpu:cpu_client_options",
        "//xla/pjrt/plugin/xla_cpu:cpu_memory",
        "//xla/pjrt/plugin/xla_cpu:xla_cpu_pjrt_client",
        "//xla/service:hlo_proto_cc",
        "//xla/tests:literal_test_util",
        "//xla/tsl/lib/core:status_test_util",
        "//xla/tsl/platform:env",
        "//xla/tsl/platform:errors",
        "//xla/tsl/platform:logging",
        "//xla/tsl/platform:statusor",
        "//xla/tsl/platform:test",
        "//xla/tsl/platform:test_benchmark",
        "//xla/tsl/platform:test_main",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:status_matchers",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/synchronization",
        "@com_google_googletest//:gtest",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:Parser",
        "@tsl//tsl/platform:path",
    ],
)

cc_library(
    name = "cpu_async_execution_tracker",
    srcs = ["cpu_async_execution_tracker.cc"],
    hdrs = ["cpu_async_execution_tracker.h"],
    deps = [
        ":cpu_event",
        "//xla/tsl/concurrency:async_value",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/synchronization",
    ],
)

cc_library(
    name = "execution_stream_event_map",
    hdrs = ["execution_stream_event_map.h"],
    deps = [
        ":cpu_event",
        "//xla/tsl/concurrency:async_value",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/synchronization",
    ],
)

cc_library(
    name = "cpu_pjrt_compiler",
    srcs = ["cpu_pjrt_compiler.cc"],
    hdrs = ["cpu_pjrt_compiler.h"],
    visibility = internal_visibility(["//xla/pjrt/cpu:legacy_cpu_client_users"]),
    deps = [
        "//xla/backends/cpu/collectives:cpu_collectives",
        "//xla/core/collectives:clique_id",
        "//xla/core/collectives:clique_key",
        "//xla/core/collectives:communicator",
        "//xla/hlo/builder:xla_computation",
        "//xla/pjrt:pjrt_client",
        "//xla/pjrt:pjrt_compiler",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt/plugin/xla_cpu:cpu_client_options",
        "//xla/pjrt/plugin/xla_cpu:cpu_topology_description",
        "//xla/pjrt/plugin/xla_cpu:xla_cpu_pjrt_client",
        "//xla/stream_executor/platform:initialize",
        "//xla/tsl/platform:statusor",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/types:span",
        "@llvm-project//mlir:IR",
        "@tsl//tsl/platform:casts",
    ],
    alwayslink = True,
)

xla_cc_test(
    name = "cpu_pjrt_compiler_test",
    srcs = ["cpu_pjrt_compiler_test.cc"],
    deps = [
        ":cpu_client",
        ":cpu_pjrt_compiler",
        "//xla:debug_options_flags",
        "//xla/backends/cpu/codegen:cpu_features",
        "//xla/hlo/builder:xla_computation",
        "//xla/hlo/testlib:hlo_hardware_independent_test_base",
        "//xla/mlir_hlo",
        "//xla/pjrt:pjrt_executable",
        "//xla/pjrt/plugin/xla_cpu:cpu_topology",
        "//xla/pjrt/plugin/xla_cpu:cpu_topology_description",
        "//xla/tsl/platform:statusor",
        "//xla/tsl/platform:test_main",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_googletest//:gtest",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:Parser",
    ],
)
